import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableView, QLineEdit, QComboBox, QPushButton, QWidget, \
    QVBoxLayout, QSizePolicy, QDialog, QDialogButtonBox,QInputDialog,QFileDialog
from PyQt5.QtGui import QStandardItem, QStandardItemModel
# from PyQt5.QtWidgets import QFileDialog
from PyQt5.QtCore import pyqtSignal, Qt


class CustomDialog(QDialog):
    data_signal = pyqtSignal(bool)
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Custom QTableView")
        self.setFixedSize( 310, 250)

        self.model = QStandardItemModel(4, 2)
        self.tableView = QTableView()
        self.tableView.setModel(self.model)

        # First row: "name"
        self.model.setItem(0, 0, QStandardItem("name"))
        self.nameLineEdit = QLineEdit()
        self.nameLineEdit.textChanged.connect(lambda text: self.update_item_text(0, 1, text))
        self.model.setItem(0, 1, QStandardItem(""))
        self.nameLineEdit.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
        self.tableView.setIndexWidget(self.model.index(0, 1), self.nameLineEdit)


        # Second row: "time"
        self.model.setItem(1, 0, QStandardItem("time"))
        self.timeLineEdit = QLineEdit()
        self.model.setItem(1, 1, QStandardItem(""))
        self.timeLineEdit.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
        self.tableView.setIndexWidget(self.model.index(1, 1), self.timeLineEdit)

        self.timeLineEdit.textChanged.connect(lambda text: self.update_item_text(1, 1, text))


        # Third row: "mission"
        self.model.setItem(2, 0, QStandardItem("mission"))
        self.missionComboBox = QComboBox(self)
        self.missionComboBox.addItems(["暂定", "播放指定音乐", "悬浮窗弹出", "语音播报"])
        self.missionComboBox.currentIndexChanged.connect(self.mission_changed)
        self.model.setItem(2, 1, QStandardItem("NULL"))
        self.missionComboBox.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
        self.tableView.setIndexWidget(self.model.index(2, 1), self.missionComboBox)

        # Fourth row: "来源"
        self.model.setItem(3, 0, QStandardItem("来源(默认为NULL)"))
        self.fromSoure = QLineEdit()
        self.model.setItem(3, 1, QStandardItem("NULL"))
        self.fromSoure.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
        self.tableView.setIndexWidget(self.model.index(3, 1), self.fromSoure)

        self.fromSoure.textChanged.connect(lambda text: self.update_item_text(3, 1, text))

        # Create OK and Cancel buttons
        button_box = QDialogButtonBox(
        QDialogButtonBox.Ok | QDialogButtonBox.Cancel, self)
        button_box.accepted.connect(self.accept_and_emit_data)
        button_box.rejected.connect(self.reject)

        # Create a layout for the main window
        layout = QVBoxLayout()
        layout.addWidget(self.tableView)

        # Add the button box to the layout
        layout.addWidget(button_box)

        self.setLayout(layout)

    def mission_changed(self, index):

        selected_option = self.missionComboBox.currentText()

        if selected_option == "语音播报":
            text, ok = QInputDialog.getText(self, "输入框", "请输入字符串:")
            if ok:
                self.missionComboBox.setCurrentIndex(-1)
                self.model.setItem(2,1,QStandardItem('播报:'+text))
                self.missionComboBox.setStyleSheet("background-color: rgba(255, 255, 255, 100);")
                print(f"用户输入的字符串为: {text}")
        elif selected_option == "播放指定音乐":
            options = QFileDialog.Options()
            options |= QFileDialog.DontUseNativeDialog
            fileName, _ = QFileDialog.getOpenFileName(self, "选择音频文件", "", "All Files (*);;Audio Files (*.mp3 *.wav)", options=options)
            if fileName:
                self.missionComboBox.setCurrentIndex(-1)
                self.model.setItem(2, 1, QStandardItem(fileName))
                self.missionComboBox.setStyleSheet("background-color: rgba(255, 255, 255, 100);")
        elif selected_option == "悬浮窗弹出":
            self.missionComboBox.setCurrentIndex(-1)
            self.model.setItem(2, 1, QStandardItem('悬浮窗弹出'))
            self.missionComboBox.setStyleSheet("background-color: rgba(255, 255, 255, 100);")
            print("悬浮窗弹出")

        elif selected_option=="暂定":
            self.missionComboBox.setCurrentIndex(-1)
            self.model.setItem(2, 1, QStandardItem('都说了暂定'))
            self.missionComboBox.setStyleSheet("background-color: rgba(255, 255, 255, 100);")

        # 如果选择了其他选项，可以添加相应的处理逻辑
        else:
            pass
        self.missionComboBox.setCurrentIndex(-1)

    def update_item_text(self,row, col, text):
        # print('sdasdasd')
        item = self.model.item(row, col)
        if item:
            item.setText(text)

    def accept_and_emit_data(self):
        # 执行当前类中的操作
        sig=True  # 你要传递的数据
        # print(self.get_table_data())
        self.data_signal.emit(sig)
        self.accept()

    def get_table_data(self):
        data = {}
        for row in range(self.model.rowCount()):
            row_data = {}
            for column in range(self.model.columnCount()):
                item = self.model.item(row, column)
                if item is not None:
                    print(item.text())
                    row_data[f"Column{column + 1}"] = item.text()
            data[f"Row{row + 1}"] = row_data
        return data


    def openFileDialog(self):
        options = QFileDialog.Options()
        file_name, _ = QFileDialog.getOpenFileName(self, "Open File", "", "All Files (*)", options=options)

        if file_name:
            self.model.setItem(2, 1, QStandardItem(file_name))
            self.missionButton.setText('')
            self.missionButton.setStyleSheet("background-color: rgba(255, 255, 255, 100);")

